package com.example.jiemian;

import com.example.jiemian.Webview_zidingyi.ScrollListener;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Title  extends Activity implements ScrollListener{

       private Webview_zidingyi WEBVIEW_SHOP;
	   private String url="http://auto.news18a.com/m/price/index/index/gwifi/";
		//默认的动画时间
		private static final int TIME_ANIMATION = 300;

		private ImageView img_bar;
		private RelativeLayout fl_top;
        private boolean isTopHide=false;
       
      //是否已经完成测量
    	private boolean isMeasured = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.title_webview_item);
		init();
		img_bar = (ImageView) findViewById(R.id.img_bar);
		fl_top = (RelativeLayout) findViewById(R.id.ll_top);
		WEBVIEW_SHOP.setScrollListener(this);
		
		ViewTreeObserver viewTreeObserver = fl_top.getViewTreeObserver();
		viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {

				if (!isMeasured) {
					RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout
							.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
					layoutParams.setMargins(0, img_bar.getHeight(), 0, 0);
					WEBVIEW_SHOP.setLayoutParams(layoutParams);
					isMeasured = true;
				}
				return true;
			}
		});
	}
	/**
	 * 显示上部的布局
	 */
	private void showTop() {
		ObjectAnimator anim4 = ObjectAnimator.ofFloat(fl_top, "y", fl_top.getY(),
				0);
		anim4.setDuration(TIME_ANIMATION);
		anim4.start();
	}


	/**
	 * 隐藏上部的布局
	 */
	private void hideTop() {
		
		ObjectAnimator anim4 = ObjectAnimator.ofFloat(fl_top, "y", 0,
				-(img_bar.getHeight()));
		anim4.setDuration(TIME_ANIMATION);
		anim4.start();
	}
	
	private  void init(){
		WEBVIEW_SHOP=(Webview_zidingyi) findViewById(R.id.WEBVIEW_SHOP);
		WebSettings webSettings = WEBVIEW_SHOP.getSettings();
		webSettings.setJavaScriptEnabled(true);		 
		webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		WEBVIEW_SHOP.loadUrl(url);
		WEBVIEW_SHOP.setWebViewClient(new WebViewClient(){
	           @Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        	   view.loadUrl(url);
	               return true;
	        }});		
		}
	@Override
	public void scrollOritention(int oritention) {
		// TODO Auto-generated method stub
		System.out.println(oritention+"oritention"+oritention);
		if(oritention==1){	//向上
			if(isTopHide){				
			}else{			
				hideTop();
				isTopHide=true;
			}	
			
		}
		if(oritention==16){//向下			
				showTop();	
				isTopHide=false;
			
			}			
		}		
}
